package com.pan.concurrency.thread;

import java.time.LocalDateTime;

/**
 * <p>
 *      守护进程： 监控整个应用下的进程，如果当前应用没有其他的进程，守护进程停止
 *
 *      注意的点：
 *          先设置这个进程为守护进程，其次再开始通知CPU开始执行run代码段
 * </p>
 *
 * @author kaiji
 * @since 2020/1/24 19:35
 */
public class DaemonThread {

    public static void main(String[] args) {
        Box box1 = new Box("001", 1);
        Box box2 = new Box("002", 4);
        Box box3 = new Box("003", 2);
        Box box4 = new Box("004", 2);
        Waiter waiter = new Waiter("服务员", 1000);

        box1.start();
        box2.start();
        box3.start();
        box4.start();

        waiter.setDaemon(true);
        waiter.start();

    }



}


class Box extends Thread {
    private String boxNo;
    private int seconds;

    public Box(String boxNo, int seconds) {
        this.boxNo = boxNo;
        this.seconds = seconds;
    }

    @Override
    public void run() {
        System.out.println(boxNo + "编号包厢，开始玩耍了！");
        try {
            Thread.sleep(seconds * 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(boxNo + "编号包厢，结账了！");
    }
}

class Waiter extends Thread {
    private String userName;
    private int seconds;

    public Waiter(String userName, int seconds) {
        this.userName = userName;
        this.seconds = seconds;
    }
    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(seconds);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(userName + "为您报时，现在的时间是：" + LocalDateTime.now().toString());
        }
    }
}
